<template>
	<scroll-view class="page">
		<xzfx-empty v-if="list.length === 0" tips="暂无置换车辆" />
		<view class="replace-card" v-for="(item, index) in list" :key="index" @click.stop="$go('/pages/car-replace/details?id=' + item.id)">
			<view class="replace-card-head">
				<view class="replace-number">
					置换编码:
					<text>{{ item.id }}</text>
				</view>
				<view class="replace-state">申请中</view>
			</view>
			<view class="replace-card-body">
				<view class="replace-info">
					<view class="label">意向品牌:</view>
					<view class="value">{{ item.purposeTruckBrand }}</view>
				</view>
				<view class="replace-info">
					<view class="label">意向车型:</view>
					<view class="value">{{ item.purposeTruckType }}</view>
				</view>
				<view class="replace-info">
					<view class="label">旧车表里程:</view>
					<view class="value">{{ item.oldShowMiles }}万公里</view>
				</view>
			</view>
			<view class="replace-action">
				<view class="time"><text>发布时间:</text>{{ item.createTime }}</view>
				<view class="btn" @click.stop="deleteFn(item)">取消申请</view>
			</view>
		</view>
		<xzfx-divider v-if="list.length > 10">{{ dividerTitle }}</xzfx-divider>
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				total: 0,
				list: [],
				pageNo: 1,
				pageSize: 10,
				dividerTitle: "暂无更多"
			};
		},
		onShow() {
			this.refreshData()
		},
		onPullDownRefresh(){
			this.refreshData()
		},
		methods: {
			refreshData(){
				this.pageNo = 1;
				this.pageSize = 10;
				this.total = 0;
				this.list = [];
				this.getDataFn()
			},
			deleteFn(item){
				this.$utils.system.loading("正在删除，请稍后");
				this.$apis.carReplace.remove({
					id: item.id
				}).then(res => {
					this.refreshData()
				}).catch(err => {
					console.log(err)
				}).finally(() => {
					uni.hideLoading();
				});
			},
			loadDataFn(){
				if(this.total > this.list.length){
					this.pageNo = this.pageNo + 1
					this.getDataFn()
				}else{
					this.dividerTitle = "暂无更多"
				}
			},
			getDataFn(){
				this.$utils.system.loading("加载中，请稍后");
				this.dividerTitle = "加载中......"
				this.$apis.carReplace.getList({
					pageNo: this.pageNo,
					pageSize: this.pageSize
				}).then(res => {
					this.dividerTitle = "暂无更多"
					this.total = res.total
					this.list = this.list.concat(res.items)
				}).catch(err => {
					console.log(err)
					this.dividerTitle = "暂无更多";
				}).finally(() => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.page{
		height: 100vh;
		padding-bottom: $xzfx-spacing;
		.replace-card{
			margin: $xzfx-spacing;
			padding: $xzfx-spacing;
			border-radius: 20rpx;
			background-color: #fff;
			.replace-card-head{
				font-size: 30rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1rpx solid #f1f1f1;
				padding-bottom: $xzfx-spacing;
				.replace-number{
					color: #333333;
					text{
						font-size: 32rpx;
						margin-left: 10rpx;
					}
				}
				.replace-state{
					color:  #1899ff;
					font-size: 28rpx;
				}
			}
			.replace-card-body{
				padding: 20rpx 0;
				.replace-info{
					display: flex;
					padding: 10rpx 0;
					line-height: 40rpx;
					.label{
						width: 160rpx;
						color: #666666;
						font-size: 30rpx;
					}
					.value{
						text-align: right;
						width: calc(100% - 160rpx);
						color: #333333;
						font-size: 30rpx;
					}
				}
			}
			.replace-action{
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-top: 1rpx solid #f1f1f1;
				padding-top: 20rpx;
				.time{
					font-size: 28rpx;
					color: #666666;
					text{
						font-size: 26rpx;
						padding-right: 10rpx;
					}
				}
				.btn{
					width: 180rpx;
					height: 66rpx;
					border-radius: 33rpx;
					color: #fff;
					font-size: 28rpx;
					background-color: $xzfx-theme;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
		}
	}
</style>
